<?php

/******************************
Copyright (c) 2009, Avanade
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Avanade nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
******************************/


$serverName = "";
$uid = "";
$pwd = "";

$connectionOptions = array("Database"=>"VirtualEarthDemo", "UID" => $uid, "PWD" => $pwd);

$conn = sqlsrv_connect( $serverName, $connectionOptions);

if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET')
{
	// Set default values if no parameters were set.  This will allow us to retrieve all push pins easily.
	$topLeftLatitude = 90;
	$topLeftLongitude = -180;
	$bottomRightLatitude = -90;
	$bottomRightLongitude = 180;
	
	// Get Parameters
	if (isset($_GET['tlLat']) && isset($_GET['tlLong']) && isset($_GET['brLat']) && isset($_GET['brLong']))
	{
	                $topLeftLatitude = $_GET['tlLat'];
	                $topLeftLongitude = $_GET['tlLong'];
	                $bottomRightLatitude = $_GET['brLat'];
	                $bottomRightLongitude = $_GET['brLong'];
	}
	
	// Check to make sure they are sane
	if (!($topLeftLatitude != null && $topLeftLongitude != null && $bottomRightLatitude != null && $bottomRightLongitude != null &&
	                 -180 <= $topLeftLongitude && $topLeftLongitude < $bottomRightLongitude && $bottomRightLongitude <= 180 &&
	                 90 >= $topLeftLatitude && $topLeftLatitude > $bottomRightLatitude && $bottomRightLatitude >= -90)){
	                echo "Invalid Parameters";
	                DIE(-1);
	}
	
	// create query object
	$tsql = "SELECT title, description, latitude, longitude FROM pins WHERE latitude <= ? AND latitude >= ? AND longitude >= ? AND longitude <= ?";

	// set up params
	$params =  array((string)$topLeftLatitude, (string)$bottomRightLatitude, (string)$topLeftLongitude, (string)$bottomRightLongitude);
	
	// execute query
	$getProducts = sqlsrv_query( $conn, $tsql, $params);
	
	$arr = array();
	
	while( $obj = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
	{
	
	$arr[] = $obj;
	
	}
	
	echo json_encode($arr);
}
else if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')
{
	$title = "";
	$description = "";
	$latitude = 0;
	$longitude = 0;

	// Get Parameters
	if (isset($_POST['title']) && isset($_POST['description']) && isset($_POST['latitude']) && isset($_POST['longitude']))
	{
		$title = $_POST['title'];
		$description = $_POST['description'];
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];
	}
	
	
	// Check to make sure they are sane, execute if correct
	if (($latitude <= 90) && ($latitude >= -90) && ($longitude <= 180) && ($longitude >= -180) && ($description != "") && ($title != ""))
	{

		$tsql1 = "INSERT INTO pins
		  (title, description, latitude, longitude) 
	          VALUES(?,?,?,?)";
	
	
		/* Execute the statement with the specified parameter values. */
		$stmt1 = sqlsrv_query( $conn, $tsql1, array($title, $description, $latitude, $longitude));
	
		if( $stmt1 == false )
		{
	  	   echo "Statement 1 could not be executed.\n";
	     		die( print_r( sqlsrv_errors(), true));
		}
	
		/* Free statement resources. */
		sqlsrv_free_stmt( $stmt1);
	               
	}
	else
	{
		echo "Invalid Parameters";
	        DIE(-1);

	}	
	
	
}
?>

